﻿using System;
using System.Configuration;
using System.Web.Configuration;
using System.Collections;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Web;
///<summary>
/// class books
/// Provides static functions to access 'book' 
/// and serves as abastraction layer between the web Page and
/// actual data providers
///</summary>
public static class books
{

    private static bool _isInitialized = false;
    private static booksProvider _provider;
    private static SmallBusinessDataProvidersSection _providersSection;

    public static booksProvider Provider
    {
        get
        {
            Initialize();
            return _provider;
        }
    }

    public static List<bookatt> GetAllBooks()
    {
        return Provider.GetAllBooks();
    }

    public static bookatt GetBooksItem(string booksItemId)
    {
        return Provider.GetBooksItem(booksItemId);
    }

    private static void Initialize()
    {
        if (!_isInitialized)
        {
            _providersSection = (ConfigurationManager.GetSection("SmallBusinessDataProviders")) as SmallBusinessDataProvidersSection;
            if (_providersSection == null)
            {
                throw new Exception(Messages.booksConfigSectionNotFound);
            }

            _provider = ProvidersHelper.InstantiateProvider(_providersSection.BooksProviders[_providersSection.BooksProviderName],
                typeof(booksProvider)) as booksProvider;

            if (_provider == null)
            {
                throw new Exception("books default provider could not be instantiated");
            }
        }
    }

}
